***Note: replicators should set directory to open data 
*cd ""


********** CLEANING DATA **********

* PART 2: Define Ethnoracial subgroups

* (a) Country of Origins

	clear all
	use Data.dta, replace
	

*Harmonize across ESS waves that have slightly different variable names for parental country of origins, beginning from father
	
	replace  fbrncntb=fbrncnt if essround==2
	replace  fbrncntb=fbrncnt if essround==3
	replace  fbrncntb=fbrncnta if essround==4
	replace  fbrncntb=fbrncnta if essround==5
	replace  fbrncntb=fbrncntc if essround==9
	
	kountry fbrncntb, from(iso2c)  marker
	rename NAMES_STD country_dad
	rename MARKER marker_country_dad
	
* Enter manually country names that ESS miscodes to adapt them to standard iso naming
	
	replace country_dad="Kosovo" if country_dad=="xk"
	replace marker_country_dad=1 if  country_dad=="Kosovo"
	replace country_dad="United Arab Emirates" if country_dad== "ae"
	replace marker_country_dad=1 if  country_dad=="United Arab Emirates"
	replace country_dad="Åland Islands" if country_dad== "ax"
	replace marker_country_dad=1 if  country_dad=="Åland Islands"
	replace country_dad="Bonaire" if country_dad== "bq"
	replace marker_country_dad=1 if  country_dad=="Bonaire"
	replace country_dad="Curaçao" if country_dad== "cw"
	replace marker_country_dad=1 if  country_dad=="Curaçao"
	replace country_dad="Saint Martin" if country_dad== "mf"
	replace marker_country_dad=1 if  country_dad=="Saint Martin"
	replace country_dad="Israel" if country_dad=="02"
	replace marker_country_dad=1 if  country_dad=="Israel"
	replace country_dad="Russian Federation" if country_dad=="Russia"
	replace country_dad="Macedonia, FYR" if country_dad=="Macedonia"
	replace country_dad="Albania" if country_dad=="Kosovo"
	replace country_dad="Iran, Islamic Rep." if country_dad=="Iran"
	replace country_dad="Yemen, Rep." if country_dad=="Yemen"
	replace country_dad="Congo, Dem. Rep" if country_dad=="Congo"
	replace country_dad="Serbia and Montenegro" if country_dad=="Yugoslavia"
	replace country_dad="Congo, Dem. Rep" if country_dad=="Democratic Republic of Congo"
	replace country_dad="Egypt, Arab Rep." if country_dad=="Egypt"
	replace country_dad="Syrian Arab Republic" if country_dad=="Syria"
	replace country_dad="Sweden" if country_dad=="Åland Islands"
	replace country_dad="Venezuela, RB" if country_dad=="Bonaire"
	replace country_dad="Brunei Darussalam" if country_dad=="Brunei"
	replace country_dad="Venezuela, RB" if country_dad=="Curaçao"
	replace country_dad="Gambia, The" if country_dad=="Gambia"
	replace country_dad="St. Lucia" if country_dad=="Guadeloupe"
	replace country_dad="Hong Kong SAR, China" if country_dad=="Hong Kong"
	replace country_dad="Lao PDR" if country_dad=="Laos"
	replace country_dad="Macao SAR, China" if country_dad=="Macao"
	replace country_dad="St. Lucia" if country_dad=="Martinique"
	replace country_dad="Serbia and Montenegro" if country_dad=="Montenegro"
	replace country_dad="West Bank and Gaza" if country_dad=="Palestine"
	replace country_dad="Madagascar" if country_dad=="Reunion"
	replace country_dad="St. Kitts and Nevis" if country_dad=="Saint Kitts and Nevis"
	replace country_dad="St. Lucia" if country_dad=="Saint Lucia"
	replace country_dad="Korea, Rep." if country_dad=="South Korea"
	replace country_dad="Taiwan, China" if country_dad=="Taiwan"
	replace country_dad="Timor-Leste" if country_dad=="Timor"
	replace country_dad="Venezuela, RB" if country_dad=="Venezuela"
	replace country_dad="" if marker_country_dad==0

* Repeat for mothers 
	
	replace  mbrncntb=mbrncnt if essround==2
	replace  mbrncntb=mbrncnt if essround==3
	replace  mbrncntb=mbrncnta if essround==4
	replace  mbrncntb=mbrncnta if essround==5
	replace  mbrncntb=mbrncntc if essround==9
	kountry mbrncntb, from(iso2c)   marker
	rename NAMES_STD country_mum
	rename MARKER marker_country_mum

	replace country_mum="Kosovo" if country_mum=="xk"
	replace marker_country_mum=1 if  country_mum=="Kosovo"
	replace country_mum="United Arab Emirates" if country_mum== "ae"
	replace marker_country_mum=1 if  country_mum=="United Arab Emirates"
	replace country_mum="Åland Islands" if country_mum== "ax"
	replace marker_country_mum=1 if  country_mum=="Åland Islands"
	replace country_mum="Bonaire" if country_mum== "bq"
	replace marker_country_mum=1 if  country_mum=="Bonaire"
	replace country_mum="Curaçao" if country_mum== "cw"
	replace marker_country_mum=1 if  country_mum=="Curaçao"
	replace country_mum="Saint Martin" if country_mum== "mf"
	replace marker_country_mum=1 if  country_mum=="Saint Martin"
	replace country_mum="Jersey" if country_mum== "je" 
	replace marker_country_mum=1 if  country_mum=="Jersey"
	replace country_mum="Isle of Man" if country_mum== "im"
	replace marker_country_mum=1 if  country_mum=="Isle of Man"
	replace country_mum="Israel" if country_mum=="02"
	replace marker_country_mum=1 if  country_mum=="Israel"
	replace country_mum="Russian Federation" if country_mum=="Russia"
	replace country_mum="Macedonia, FYR" if country_mum=="Macedonia"
	replace country_mum="Albania" if country_mum=="Kosovo"
	replace country_mum="Iran, Islamic Rep." if country_mum=="Iran"
	replace country_mum="Yemen, Rep." if country_mum=="Yemen"
	replace country_mum="Congo, Dem. Rep" if country_mum=="Congo"
	replace country_mum="Serbia and Montenegro" if country_mum=="Yugoslavia"
	replace country_mum="Congo, Dem. Rep" if country_mum=="Democratic Republic of Congo"
	replace country_mum="Egypt, Arab Rep." if country_mum=="Egypt"
	replace country_mum="Syrian Arab Republic" if country_mum=="Syria"
	replace country_mum="Sweden" if country_mum=="Åland Islands"
	replace country_mum="Venezuela, RB" if country_mum=="Bonaire"
	replace country_mum="Brunei Darussalam" if country_mum=="Brunei"
	replace country_mum="Venezuela, RB" if country_mum=="Curaçao"
	replace country_mum="Gambia, The" if country_mum=="Gambia"
	replace country_mum="St. Lucia" if country_mum=="Guadeloupe"
	replace country_mum="Hong Kong SAR, China" if country_mum=="Hong Kong"
	replace country_mum="Lao PDR" if country_mum=="Laos"
	replace country_mum="Macao SAR, China" if country_mum=="Macao"
	replace country_mum="St. Lucia" if country_mum=="Martinique"
	replace country_mum="Serbia and Montenegro" if country_mum=="Montenegro"
	replace country_mum="West Bank and Gaza" if country_mum=="Palestine"
	replace country_mum="Madagascar" if country_mum=="Reunion"
	replace country_mum="St. Kitts and Nevis" if country_mum=="Saint Kitts and Nevis"
	replace country_mum="St. Lucia" if country_mum=="Saint Lucia"
	replace country_mum="Korea, Rep." if country_mum=="South Korea"
	replace country_mum="Taiwan, China" if country_mum=="Taiwan"
	replace country_mum="Timor-Leste" if country_mum=="Timor"
	replace country_mum="Venezuela, RB" if country_mum=="Venezuela"
	replace country_mum="" if marker_country_mum==0

* Repeat for sampled individual  

	replace  cntbrthc=cntbrtha if essround==2
	replace  cntbrthc=cntbrtha if essround==3
	replace  cntbrthc=cntbrthb if essround==4
	replace  cntbrthc=cntbrthb if essround==5
	replace  cntbrthc=cntbrthd if essround==9
	kountry cntbrthc, from(iso2c)    marker
	rename NAMES_STD country_born
	rename MARKER marker_country_born

	replace country_born="Kosovo" if country_born=="xk"
	replace marker_country_born=1 if  country_born=="Kosovo"
	replace country_born="United Arab Emirates" if country_born== "ae"
	replace marker_country_born=1 if  country_born=="United Arab Emirates"
	replace country_born="Åland Islands" if country_born== "ax"
	replace marker_country_born=1 if  country_born=="Åland Islands"
	replace country_born="Bonaire" if country_born== "bq"
	replace marker_country_born=1 if  country_born=="Bonaire"
	replace country_born="Curaçao" if country_born== "cw"
	replace marker_country_born=1 if  country_born=="Curaçao"
	replace country_born="Saint Martin" if country_born== "mf"
	replace marker_country_born=1 if  country_born=="Saint Martin"
	replace country_born="Jersey" if country_born== "je" 
	replace marker_country_born=1 if  country_born=="Jersey"
	replace country_born="Isle of Man" if country_born== "im" 
	replace marker_country_born=1 if  country_born=="Isle of Man"
	replace country_born="Israel" if country_born=="02"
	replace marker_country_born=1 if  country_born=="Israel"
	replace country_born="Russian Federation" if country_born=="Russia"
	replace country_born="Macedonia, FYR" if country_born=="Macedonia"
	replace country_born="Albania" if country_born=="Kosovo"
	replace country_born="Iran, Islamic Rep." if country_born=="Iran"
	replace country_born="Yemen, Rep." if country_born=="Yemen"
	replace country_born="Congo, Dem. Rep" if country_born=="Congo"
	replace country_born="Serbia and Montenegro" if country_born=="Yugoslavia"
	replace country_born="Congo, Dem. Rep" if country_born=="Democratic Republic of Congo"
	replace country_born="Egypt, Arab Rep." if country_born=="Egypt"
	replace country_born="Syrian Arab Republic" if country_born=="Syria"
	replace country_born="Sweden" if country_born=="Åland Islands"
	replace country_born="Venezuela, RB" if country_born=="Bonaire"
	replace country_born="Brunei Darussalam" if country_born=="Brunei"
	replace country_born="Venezuela, RB" if country_born=="Curaçao"
	replace country_born="Gambia, The" if country_born=="Gambia"
	replace country_born="St. Lucia" if country_born=="Guadeloupe"
	replace country_born="Hong Kong SAR, China" if country_born=="Hong Kong"
	replace country_born="Lao PDR" if country_born=="Laos"
	replace country_born="Macao SAR, China" if country_born=="Macao"
	replace country_born="St. Lucia" if country_born=="Martinique"
	replace country_born="Serbia and Montenegro" if country_born=="Montenegro"
	replace country_born="West Bank and Gaza" if country_born=="Palestine"
	replace country_born="Madagascar" if country_born=="Reunion"
	replace country_born="St. Kitts and Nevis" if country_born=="Saint Kitts and Nevis"
	replace country_born="St. Lucia" if country_born=="Saint Lucia"
	replace country_born="Korea, Rep." if country_born=="South Korea"
	replace country_born="Taiwan, China" if country_born=="Taiwan"
	replace country_born="Timor-Leste" if country_born=="Timor"
	replace country_born="Venezuela, RB" if country_born=="Venezuela"
	replace country_born=country if marker_country_born==0

*Generate macro-level geographic indicators, boiling down to continentsm then create groups

	 drop GEO
	 kountry country_mum, from(other) geo(un)    
	 rename GEO geo_mum
	 drop NAMES_STD 
	 kountry country_dad, from(other) geo(un)    
	 rename GEO geo_dad
	 drop NAMES_STD 
	 kountry country_born, from(other) geo(un)    
	 rename GEO geo

	 
	 gen Africa=0
	 replace Africa=1 if geo_dad=="Africa" |  geo_mum=="Africa" |  geo=="Africa"
	 
	 gen Asia=0
	 replace Asia=1 if geo_dad=="Asia" |  geo_mum=="Asia"   |  geo=="Asia"
	 
	 gen Americas=0
	 replace Americas=1 if geo_dad=="Americas" |  geo_mum=="Americas" |  geo=="Americas"
	 
	 gen Oceania=0 
	 replace Oceania=1 if geo_dad=="Oceania" |  geo_mum=="Oceania" |  geo=="Oceania"
	
	 gen Europe=0
	 replace Europe=1 if geo_dad=="Europe" |  geo_mum=="Europe"  |   geo=="Europe" 
 
*Here I create some extra-dummies that identify more granular sub-continents for the purpose of robustness.
	 
	 gen EEurope=0
	 replace EEurope=1 if country_dad=="Albania" |   country_dad=="Belarus" | country_dad=="Bulgaria"  |  country_dad=="Czech Republic"   |   country_dad=="Hungary"   |  country_dad=="Moldova"      |  country_dad=="Poland"   |  country_dad=="Romania"  | country_dad=="Russia" 	|   country_dad=="Bulgaria"   | country_dad=="Slovak Republic" |  country_dad=="Ukraine" |   country_dad=="Estonia"  |   country_dad=="Latvia" | country_dad=="Lithuania" |   country_dad=="Bosnia and Herzegovina" |  country_dad=="Russian Federation"    |  country_dad=="Slovak Republic"  |  country_dad=="Slovenia"  |  country_dad=="Macedonia, FYR" |   country_dad=="Serbia and Montenegro" |   country_dad=="Uzbekistan" |   country_dad=="Turkmenistan" |   country_dad=="Tajikistan" |   country_dad=="Kyrgyz Republic"  |   country_dad=="Kazakhstan"  |   country_dad=="Georgia" |   country_dad=="Azerbaijan"
	 
	replace EEurope=1 if country_mum=="Albania" | country_mum=="Belarus" |  country_mum=="Bulgaria"  |  country_mum=="Czech Republic"   |   country_mum=="Hungary"   |  country_mum=="Moldova"      |  country_mum=="Poland"   |  country_mum=="Romania"  | country_mum=="Russia" 	|   country_mum=="Bulgaria"   | country_mum=="Slovak Republic" |  country_mum=="Ukraine" |   country_dad=="Estonia"  |   country_dad=="Latvia" | country_mum=="Lithuania" |   country_mum=="Bosnia and Herzegovina" |  country_mum=="Russian Federation"    |  country_mum=="Slovak Republic"  |  country_mum=="Slovenia"  |  country_mum=="Macedonia, FYR" |   country_mum=="Serbia and Montenegro" |   country_mum=="Uzbekistan" |   country_mum=="Turkmenistan" |   country_mum=="Tajikistan" |   country_mum=="Kyrgyz Republic"  |   country_mum=="Kazakhstan"  |   country_mum=="Georgia" |   country_mum=="Azerbaijan"
	  
	replace EEurope=1 if country_born=="Albania" |  country_born=="Belarus" |country_born=="Bulgaria"  |  country_born=="Czech Republic"   |   country_born=="Hungary"   |  country_born=="Moldova"      |  country_born=="Poland"   |  country_born=="Romania"  | country_born=="Russia" 	|   country_dad=="Bulgaria"   | country_born=="Slovak Republic" |  country_born=="Ukraine" |   country_born=="Estonia"  |   country_born=="Latvia" | country_born=="Lithuania" |   country_born=="Bosnia and Herzegovina" |  country_born=="Russian Federation"    |  country_born=="Slovak Republic"  |  country_born=="Slovenia"  |  country_born=="Macedonia, FYR" |   country_born=="Serbia and Montenegro" |   country_born=="Uzbekistan" |   country_born=="Turkmenistan" |   country_born=="Tajikistan" |   country_born=="Kyrgyz Republic"  |   country_born=="Kazakhstan"  |   country_born=="Georgia" |   country_born=="Azerbaijan"
	
gen Est=0
replace Est=1 if  cntry=="CZ" | cntry=="EE" | cntry=="HR" |  cntry=="LT"|  cntry=="PL"| cntry=="RU"|   cntry=="SI" |  | cntry=="UA" | cntry=="SK"


* (b) Religious Minorities
	   
	gen Muslim=0
	replace Muslim=1 if  rlgdnm==6  
	gen Jew=0
	replace Jew=1 if  rlgdnm==5
	gen Other=0 
	replace Other=1 if  rlgdnm==7 |   rlgdnm==8

* (c) Ethnic Minorities  as self-reported

g Minority=0 if blgetmg==2
replace Minority=1 if blgetmg==1
 
* Now that we know who are 1st and 2nd generation immigrants, plus ethnic minorites, we can create a dummy identifying essentially what the paper calls "subordinate ethnoracial group" 

gen Ingroup=1  
replace Ingroup=0 if Minority==1
replace Ingroup=0 if brncntr==2
replace Ingroup=0 if facntr==2
replace Ingroup=0 if mocntr==2
replace Ingroup=0 if Jew==1
replace Ingroup=0 if Muslim==1
replace Ingroup=0 if Other==1
replace Ingroup=0 if Minority==1
replace Ingroup=0 if Asia==1
replace Ingroup=0 if Africa==1
replace Ingroup=0 if Americas==1

gen Group=.
replace Group=1 if Africa==1 
replace Group=2 if Americas==1 
replace Group=3 if Asia==1
replace Group=4 if Europe==1 & Africa==0 & Asia==0 & Americas==0  & Oceania==0  & EEurope==0
replace Group=. if Asia==1 & Africa==1
replace Group=. if Americas==1 & Africa==1
replace Group=. if Americas==1 & Asia==1

 
label define Groups 1 "Africa" 2 "Americas" 3 "Asia"  4 "Europe"
label values Group Groups 

la var Group "Ethnoracial group"
la def a  1 "Africa" 2 "Americas" 3 "Asia" 4 "Europe", modify
la val Group a

la var Muslim Muslim
la def b 0 "No" 1 "Yes"
la val Muslim b

la var Jew Jew
la def b2 0 "No" 1 "Yes"
la val Jew b2

* (d) Gender

g Female = gndr==2 if gndr!=.

la var Female "Sex"
la def asas 1 "Male" 2 "Female", modify
la val Female asas


save Data.dta, replace
 
 
 
 